package Year22_Month06.day28;

import java.util.Arrays;

public class 数组中出现次数超过一半的数字 {
    public int MoreThanHalfNum_Solution1(int [] array) {
        Arrays.sort(array);
        return array[array.length/2];
    }

    public int MoreThanHalfNum_Solution2(int [] array) {
        int count = 0;
        int key = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if(count == 0){
                key = array[i];
                count++;
            }else{
                if(array[i] == key){
                    count++;
                }else{
                    count--;
                }
            }
        }
        return key;
    }
}
